<template>
  <div class='container'>
    <el-dialog :title="title" :visible="dialogVisible" width="400px">
      <el-form ref="form" size="small" label-width="80px" :model="directoryForm" :rules="rules">
        <el-form-item label="所属学科" v-if="!$route.query.id" prop="subjectID">
          <el-select v-model="directoryForm.subjectID" style="width:100%">
            <el-option
              v-for="item in subjectOptions"
              :value="item.value"
              :label="item.label"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="directoryForm.directoryName" placeholder="请输入目录名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button @click="submit()" type="primary">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '新增目录'
    }
  },
  async created () {
    const { data } = await simple()
    console.log(data)
    this.subjectOptions = data
  },
  data () {
    return {
      directoryForm: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { type: 'number', message: '请选择所属学科', tirgger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', tirgger: 'blur' }
        ]
      },
      subjectOptions: []
    }
  },
  methods: {
    // 关闭对话框
    closeDialog () {
      this.$emit('update:dialogVisible', false)
      this.directoryForm = {
        subjectID: '',
        directoryName: ''
      }
    },
    // 提交对话框
    submit () {
      this.$refs.form.validate(async boo => {
        if (boo) {
          if (this.directoryForm.id) {
            await update(this.directoryForm)
          } else {
            await add(this.directoryForm)
          }
          this.closeDialog()
          this.$emit('reload')
        }
      })
    }
  }

}
</script>

<style scoped lang='less'></style>
